package com.pub.file;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUploadServlet extends HttpServlet {

	// 定义文件的上传路径
	private String uploadPath = "D:\\flexupload\\";
	// 限制文件的上传大小
	private int maxPostSize = 100 * 1024 * 1024;
	public FileUploadServlet() {
		super();
	}
	public void destroy() {
		super.destroy();
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("----------------开始上传。。。。。。。。。。。。");
		// 得到用户需要保存的服装的id
		String dressId = request.getParameter("dressID");
		System.out.println(dressId);

		// 保存文件到服务器中
		response.setContentType("text/html; charset=UTF-8");
		DiskFileItemFactory factory = new DiskFileItemFactory();
		factory.setSizeThreshold(4096);
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setSizeMax(maxPostSize);
		try {
			List fileItems = upload.parseRequest(request);
			Iterator iter = fileItems.iterator();
			while (iter.hasNext()) {
				FileItem item = (FileItem) iter.next();
				if (!item.isFormField()) {
					String name = item.getName();
					System.out.println(name);
					try {
						item.write(new File(uploadPath + name));
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		}

	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("开始。。。。。。。");
		//this.doGet(request, response);
		processRequest(request, response);
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		System.out.println("初始化。。。。servlet");
	}
	/**
	 * 私有方法 文件上传
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void processRequest(HttpServletRequest request, HttpServletResponse response)   
	           throws ServletException, IOException {   
	        System.out.println("Access !");   
	        response.setContentType("text/html;charset=UTF-8");   
        PrintWriter out = response.getWriter();   
	  
	//保存文件到服务器中   
	 
	        DiskFileItemFactory factory = new DiskFileItemFactory();   
        factory.setSizeThreshold(4096);   
        ServletFileUpload upload = new ServletFileUpload(factory);   
	       upload.setSizeMax(maxPostSize);   
	       try {   
	           List fileItems = upload.parseRequest(request);   
	            Iterator iter = fileItems.iterator();   
	            while (iter.hasNext()) {   
	                FileItem item = (FileItem) iter.next();   
	                if (!item.isFormField()) {   
	                    String name = item.getName();   
	                    System.out.println(name);   
	                  try {   
                        item.write(new File(uploadPath + name));   
	                       // SaveFile s = new SaveFile();   
	 
	                       // s.saveFile(name);   
	  
	                    } catch (Exception e) {   
	                        e.printStackTrace();   
	                    }   
	                }   
            }   
	       } catch (FileUploadException e) {   
            e.printStackTrace();   
	            System.out.println(e.getMessage() + "结束");   
	       }   
	   }   
	
}
